package org.aeon.client.view.components.listColumn;

import lombok.extern.slf4j.Slf4j;
import org.aeon.client.context.GlobalContext;
import org.aeon.client.view.ApplicationUI;
import org.aeon.client.view.components.button.CheckButton;
import org.aeon.client.view.components.button.ContactButton;
import org.aeon.client.view.components.button.ExpansionButton;
import org.aeon.client.view.components.button.OperationButton;
import org.aeon.client.view.components.sessionPanel.GroupInfoPanel;
import org.aeon.client.view.components.sessionPanel.SessionPanel;
import org.aeon.client.view.components.textField.SearchField;
import org.aeon.constant.SessionType;
import org.aeon.entity.User;
import org.aeon.entity.dto.ChatGroupDto;
import org.aeon.entity.dto.UserDto;
import org.aeon.message.request.GroupCreateRequestMessage;
import org.aeon.message.request.ListQueryRequestMessage;
import org.aeon.message.request.SearchRequestMessage;
import org.aeon.utils.SimpleRequestUtil;
import org.aeon.utils.SwingUtil;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static org.aeon.client.context.MessageMap.MessageContextKey.*;


/**
 * @author: MissingAeon
 * @date: 2024/2/20 13:57
 * @description:
 */
@Slf4j
public class SearchListPanel extends JPanel {
    private final SessionPanel sessionPanel;

    public SearchListPanel(SessionPanel sessionPanel) {
        this.sessionPanel = sessionPanel;
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        this.add(Box.createVerticalStrut(10));
        final Box topBox = Box.createHorizontalBox();
        topBox.add(Box.createHorizontalGlue());
        // 创建搜索输入框
        final SearchField searchField = new SearchField(new Dimension(240, 30));
        searchField.setPlaceholder("全网查询好友和群聊...");
        topBox.add(searchField);
        topBox.add(Box.createHorizontalStrut(10));
        topBox.add(ExpansionButton.createExpansionButton());
        topBox.add(Box.createHorizontalGlue());
        this.add(topBox);

        this.add(Box.createVerticalStrut(5));

        final JLabel promptLabel = new JLabel("搜索中...");
        promptLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        promptLabel.setVisible(false);
        this.add(promptLabel);

        // 创建一个带有内边距的空边框
        Border paddingBorder = BorderFactory.createEmptyBorder(5, 20, 0, 0);

        final Box userLabelBox = Box.createHorizontalBox();
        // 将边框应用于标签
        userLabelBox.setBorder(paddingBorder);
        final String userLabelText = "匹配的用户：";
        final JLabel userLabel = new JLabel(userLabelText);
        userLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
        userLabelBox.add(userLabel);
        userLabelBox.add(Box.createHorizontalGlue());
        this.add(userLabelBox);

        final Dimension dimension = new Dimension(296, 300);
        final ColumnScrollPanel userScroll = new ColumnScrollPanel(dimension);
        this.add(userScroll);

        final Box groupLabelBox = Box.createHorizontalBox();
        groupLabelBox.setBorder(paddingBorder);
        final String groupLabelText = "匹配的群聊：";
        final JLabel groupLabel = new JLabel(groupLabelText);
        groupLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
        groupLabelBox.add(groupLabel);
        groupLabelBox.add(Box.createHorizontalGlue());
        this.add(groupLabelBox);

        final ColumnScrollPanel groupScroll = new ColumnScrollPanel(dimension);
        this.add(groupScroll);

        // 添加文本变化事件监听器
        searchField.addDocumentListenerHandle(() -> {
            final String keyword = searchField.getText();
            // 清理
            userScroll.clean();
            groupScroll.clean();
            if (keyword == null || keyword.isEmpty()) {
                userLabel.setText(userLabelText);
                groupLabel.setText(groupLabelText);
                return;
            }
            // 提示显示
            promptLabel.setVisible(true);
            final GlobalContext context = GlobalContext.getContext();
            // 准备消息
            final SearchRequestMessage reqMsg = new SearchRequestMessage(keyword);
            SimpleRequestUtil.sendAndWait(context, reqMsg, () -> {
                SwingUtilities.invokeLater(() -> {
                    // 获取数据
                    final List<UserDto> searchUserList = context.getMessageMap().del(SearchUserList);
                    final List<ChatGroupDto> searchGroupList = context.getMessageMap().del(SearchGroupList);
                    // 提示数量
                    userLabel.setText(userLabelText + "\t\t\t" + searchUserList.size());
                    groupLabel.setText(groupLabelText + "\t\t\t" + searchGroupList.size());

                    // 展示
                    searchUserList.forEach(item -> {
                        ContactButton button = new ContactButton(item.getNickname(), sessionPanel);
                        if (item.isLeave()) {
                            button.setColorFade();
                        }
                        button.setAdditionalInfo(item.getId(), SessionType.USER_SESSION);
                        userScroll.addItem(button);
                    });
                    searchGroupList.forEach(item -> {
                        ContactButton button = new ContactButton(item.getGroupName(), sessionPanel);
                        button.setAdditionalInfo(item.getId(), SessionType.GROUP_SESSION);
                        groupScroll.addItem(button);
                    });
                    promptLabel.setVisible(false);
                });
            });
        });
    }

}
